package mchecca.controller;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;

import mchecca.model.DecisionLinesEvent;
import mchecca.model.Edge;
import mchecca.view.DecisionLinesForm;
import mchecca.view.LinesCanvas;

/**
 * Class for handling lines canvas GUI events
 * <p>
 * The {@link LinesController} is responsible for doing the following
 * <ul>
 * 	<li>Adding an edge when a point between two lines is clicked</li>
 * 	<li>Alerting the user an invalid edge has tried to be added</li>
 * 	<li>Showing the prioritized list of choices once the last edge is added</li>
 * </ul>
 * @author Michael Checca
 */
public class LinesController implements MouseListener {
	/** {@link DecisionLinesForm} to update */
	DecisionLinesForm dlForm;
	/** {@link DecisionLinesEvent} to update */
	DecisionLinesEvent dlEvent;
	/** Keeps track of the number of edges added */
	int addedEdges;

	/**
	 * Create a new lines controller
	 * @param dlForm {@link DecisionLinesForm} to control
	 * @param dlEvent {@link DecisionLinesEvent} to control
	 */
	public LinesController(DecisionLinesForm dlForm, DecisionLinesEvent dlEvent) {
		this.dlForm = dlForm;
		this.dlEvent = dlEvent;
		this.addedEdges = 0;
	}

	/**
	 * Occurs when the mouse is clicked on the {@link LinesCanvas}.
	 * <p>
	 * This method is responsible for checking the validity of edges
	 * attempting to be added.
	 * Once all edges are added, it shows the prioritized list of chocies
	 * @param e {@link MouseEvent} that initiated the click
	 */
	@Override
	public void mouseClicked(MouseEvent e) {
		dlForm.getErrorLabel().setText("");
		int space = dlForm.getLinesCanvas().getWidth() / dlEvent.getNumChoices();
		int offset = space / 2;
		int line = 0;
		for (int i = 0; i < dlEvent.getNumChoices(); i++)
			if (e.getX() < (space * i + offset) && e.getX() > (space * i - offset))
				line = i;
		if (e.getX() < offset)
			line = 0;
		if (e.getX() > (dlForm.getLinesCanvas().getWidth() - offset))
			line = dlEvent.getNumChoices();
		int height = e.getY();
		if (line != 0 && line != dlEvent.getNumChoices()) {
			Edge edge = new Edge(line-1, line, height);
			if (!dlEvent.addEdge(edge)) {
				dlForm.getErrorLabel().setText("Desired edge too close to existing edge");
				return;
			}
		} else {
			dlForm.getErrorLabel().setText("Please click between two lines");
			return;
		}
		addedEdges++;
		int edgesRemaining = (dlEvent.getNumChoices() * dlEvent.getNumRounds()) - addedEdges;
		dlForm.getInformativeMessage().setText(String.format("Edges remaining: %d", edgesRemaining));
		dlForm.getLinesCanvas().repaint();
		if (addedEdges == dlEvent.getNumChoices() * dlEvent.getNumRounds())
			showResults();
	}

	/** Shows the prioritized choices on the main DecisionLinesForm.
	 * <p>
	 * The current {@link LinesCanvas} is hidden so that the new
	 * prioritized list of choices panel may be shown
	 */
	public void showResults() {
		dlForm.getLinesCanvas().setEnabled(false);
		dlForm.getInformativeMessage().setText("");
		dlForm.getLinesCanvas().setVisible(false);
		dlForm.getDecisionsPanel().setVisible(true);
		String results = "<html>Prioritized Choices<br><br>";
		for (String s : dlEvent.getPrioritizedChoices())
			results = String.format("%s<br>%s", results, s);
		results = String.format("%s</html>", results);
		for (Component c : dlForm.getDecisionsPanel().getComponents())
			if (c instanceof JLabel)
				((JLabel) c).setText(results);
	}

	@Override
	public void mousePressed(MouseEvent e) { }

	@Override
	public void mouseReleased(MouseEvent e) { }

	@Override
	public void mouseEntered(MouseEvent e) { }

	@Override
	public void mouseExited(MouseEvent e) {	}
}
